<template>
  <div class="car">
    <!--标题B-->
    <div class="pagetitle">
      <div class="pageText">
        <span>人员统计</span>
      </div>
    </div>
    <!--标题E-->

    <!--条件-->
    <div class="carcondition">
      <el-select v-model="type" @change="isyear">
        <el-option label="月" value="yue"></el-option>
        <el-option label="年" value="nian"></el-option>
      </el-select>
      <div style="width: .2rem;"></div>
      <el-date-picker
        v-model="time"
        clear-icon=""
        @change="gettime"
        :type="timetype">
      </el-date-picker>

    </div>
    <!--条件-->

    <div style="margin-top: .2rem;">
      <div id="charts1">

      </div>
      <div id="charts2">

      </div>
    </div>

  </div>
</template>
<script>
  import api from '../../utils/api'
  import http from '../../utils/http'
  export default {
    data() {
      return {
        type: "yue",
        time: "",
        timetype: "month",
        pltext: "选择月",
        areaId: "",
        initialstr: ""
      }
    },
    mounted() {
      this.getgateWarningCount();
    },
    created() {
      this.areaId = sessionStorage.getItem('areaId');
      this.time = new Date();
      var time = new Date();
      let ys = time.getFullYear();
      let ms = time.getMonth() + 1;
      if (ms >= 1 && ms <= 9) {
        ms = "0" + ms;
      }
      this.initialstr = ys + '-' + ms;
    },
    methods: {
      gettime(data){
        if (data != null) {
          if (this.type == 'yue') {
            let y = data.getFullYear();
            let m = data.getMonth() + 1;
            if (m >= 1 && m <= 9) {
              m = "0" + m;
            }
            this.initialstr = y + "-" + m;
          } else {
            let y = data.getFullYear();
            this.initialstr = y
          }
        }
        this.getgateWarningCount();
      },
      getgateWarningCount(){
        var json = {
          modelType: "user",
          timeStr: this.initialstr,
          areaId: this.areaId
        };
        var vm = this;
        (async function () {
          let res = await http.post(api.car.gateWarningCount, json)
          if (res.data.code == '200') {
            vm.demo1(res.data.data.keyItems, res.data.data.blackCount.valueItems, res.data.data.unknownCount.valueItems, res.data.data.whiteCount.valueItems)
            let obj = res.data.data.sexCount, darr = [];
            for (var key in  obj) {
              let z = {value: obj[key], name: key}
              darr.push(z);
            }
            vm.demo2(darr);
          }
        })()
      },
      demo1(kt, black, unknown, whiteCount){
        var vm = this;
        let myChart = this.$echarts.init(document.getElementById('charts1'));
        var option = {
//          title: {
//            text: "黑名单10" + "   " + "白名单陌生车辆"
//          },
          tooltip: {
            trigger: 'axis'
          },
          legend: {
            data: ['黑名单', '白名单', '陌生人']
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: {
            type: 'category',
            boundaryGap: false,
            data: kt
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              name: '黑名单',
              type: 'line',
              stack: '总量',
              data: black
            },
            {
              name: '白名单',
              type: 'line',
              stack: '总量',
              data: whiteCount
            },
            {
              name: '陌生人',
              type: 'line',
              stack: '总量',
              data: unknown
            },
          ]
        };
        myChart.setOption(option);
        setTimeout(function () {
          myChart.resize();
        }, 10)
        window.addEventListener("resize", function () {
          myChart.resize();
        });
      },
      demo2(obj){
        var vm = this;
        let myChart = this.$echarts.init(document.getElementById('charts2'));
        var option = {
          title: {
            text: '人员性别统计',
            left: 'center'
          },
          tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
          },
          series: [
            {
              name: '人员性别',
              type: 'pie',
              radius: '55%',
              center: ['50%', '60%'],
              data: obj,
              emphasis: {
                itemStyle: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              }
            }
          ]
        };
        myChart.setOption(option);
        setTimeout(function () {
          myChart.resize();
        }, 10)
        window.addEventListener("resize", function () {
          myChart.resize();
        });
      },
      isyear(){
        if (this.type == 'yue') {
          this.timetype = "month"
          var time = new Date();
          let ys = time.getFullYear();
          let ms = time.getMonth() + 1;
          if (ms >= 1 && ms <= 9) {
            ms = "0" + ms;
          }
          this.initialstr = ys + '-' + ms;
        }
        if (this.type == 'nian') {
          this.timetype = "year"
          var time = new Date();
          let ys = time.getFullYear();
          this.initialstr = ys
        }
        this.time = new Date();
        this.getgateWarningCount();
      }
    }
  }
</script>
<style scoped>
  @import "../../../static/css/car.css";
</style>
